
const db = require('db');
const { toUpdate } = require('tools/sqlString');

module.exports = async ctx => {
    const { connection, comply } = db.init();
    try {
        const id = ctx.params.id;
        await update(id, comply);
        await updataOrders(id, comply)
        ctx.status = 200;
        ctx.body = 'ok';
    } catch (error) {
        ctx.status = 500;
        ctx.body = error
    }
    connection.end();
}

async function update(id, comply) {
    comply(`
        UPDATE settlement SET ${toUpdate({ settlementState: '待审核', trackingNo: 'null', invoiceNo: 'null', invoiceTime: 'null', repaymentTime: 'null' })}
        WHERE id = ${id}
    `)
}

/** 更新订单 */
async function updataOrders(id, comply) {
    comply(`
        UPDATE orders, settlementEntry
        SET orderStatus="结算中"
        WHERE settlementEntry.settlementId = ${id} AND settlementEntry.ordersId = orders.id
    `)
}